package com.zhl.study.service.chainResponsibility;

import cn.hutool.core.lang.Assert;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description TODO
 * @Author: z156940l@outlook.com
 * @className ProductChainSpringFactory
 * @Date: 2024/12/26 21:28
 */
@Component
public class ProductChainSpringFactory<T, R> {

    private final IHandler<T, R> firstHandler;

    public ProductChainSpringFactory(List<IHandler<T, R>> handlers) {
        Assert.notEmpty(handlers, "无责任链实现的bean！");
        firstHandler = handlers.get(0);
        for (int i = 0; i < handlers.size()-1; i++) {
            handlers.get(i).setNextHandler(handlers.get(i+1));
        }
    }

    public R executeHandler(T t) {
        return firstHandler.handler(t);
    }
}
